package org.kzwl.service.web.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.SaveCuCarouselBO;
import org.kzwl.entity.web.CuCarousel;
import org.kzwl.mapper.web.CuCarouselMapper;
import org.kzwl.service.web.CuCarouselService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张国伟/郭家恒
 * @since 2020-12-22
 */
@Service
public class CuCarouselServiceImpl extends ServiceImpl<CuCarouselMapper, CuCarousel> implements CuCarouselService {


    @Override
    public ResponseData<IPage<CuCarousel>> queryPage(SaveCuCarouselBO cuCarouselBO) {
        LambdaQueryWrapper<CuCarousel> eq = new QueryWrapper<CuCarousel>()
                .lambda()
                .eq(cuCarouselBO.getCuCarouselType() == 0 ,CuCarousel::getCuTownId, cuCarouselBO.getCuTownId())
                .eq(CuCarousel::getCuCarouselType, cuCarouselBO.getCuCarouselType());
        Page<CuCarousel> carouselPage = this.baseMapper.selectPage(new Page<>(cuCarouselBO.getCurrent(), cuCarouselBO.getSize()), eq);
        return new ResponseData<>(carouselPage);
    }

    @Override
    public ResponseData<Void> saveOne(SaveCuCarouselBO cuCarouselBO) {
        CuCarousel cuCarousel = new CuCarousel();
        BeanUtils.copyProperties(cuCarouselBO,cuCarousel);
        if (cuCarouselBO.getCuCarouselType() == 0 && cuCarouselBO.getCuTownId() == null)
        {
                throw new BizException("场馆信息错误");
        }else if (cuCarouselBO.getCuCarouselType() != 0)
        {
            cuCarousel.setCuTownId(0);
        }
        Integer integer = this.baseMapper.selectCount(new QueryWrapper<CuCarousel>().lambda().eq(CuCarousel::getCuCarouselType,cuCarouselBO.getCuCarouselType())
                .eq(CuCarousel::getCuTownId,cuCarouselBO.getCuTownId()));
        if (integer >= 5){
             throw new BizException("录播图最多为5张");
        }


        this.save(cuCarousel);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<Void> updateOne(SaveCuCarouselBO cuCarouselBO) {
        if (cuCarouselBO.getCuCarouselType() != 0)
        {
            if (cuCarouselBO.getCuTownId() == null)
            {
                throw new BizException("场馆信息错误");
            }
        }
        CuCarousel cuCarousel = new CuCarousel();
        BeanUtils.copyProperties(cuCarouselBO,cuCarousel);
        this.updateById(cuCarousel);
        return new ResponseData<>();
    }


    @Override
    public ResponseData<CuCarousel> queryOne(Long Id) {
        CuCarousel cuCarousel = this.baseMapper.selectById(Id);
        if (ObjectUtil.isNull(cuCarousel))
        {
            throw new BizException("查无此信息");
        }

        return new ResponseData<>(this.baseMapper.selectById(Id));
    }

    @Override
    public ResponseData<Void> deleteId(Long Id) {
        CuCarousel cuCarousel = this.baseMapper.selectById(Id);
        if (ObjectUtil.isNull(cuCarousel))
        {
            throw new BizException("查无此信息");
        }
        this.baseMapper.deleteById(Id);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<List<CuCarousel>> queryList(Long Id) {
        List<CuCarousel> list = this.list(new QueryWrapper<CuCarousel>().lambda().eq(CuCarousel::getCuTownId, Id));
        return new ResponseData<>(list);
    }
}
